﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.IO;

namespace BusinessAccessLayer
{
    public class BASanPham
    {
        QuanLyBanHangDataContext dbs = new QuanLyBanHangDataContext();
        SanPham sp = new SanPham();
        public DataTable getTable()
        {

            DataTable dt = new DataTable();
            dt.Columns.Add("MaSP",typeof(string));
            dt.Columns.Add("TenSP", typeof(string));
            dt.Columns.Add("Đơn Vị Tính", typeof(string));
            dt.Columns.Add("Đơn Giá", typeof(Double));
            dt.Columns.Add("Mã Loại Sản Phẩm", typeof(string));
          
            foreach (var t in dbs.SanPhams.ToList())
            {
                DataRow dr = dt.NewRow();
                dr["MaSP"] = t.MaSP;
                dr["TenSP"] = t.TenSP;
                dr["Đơn Vị Tính"] = t.DonViTinh;
                dr["Đơn Giá"] = t.DonGia;
                dr["Mã Loại Sản Phẩm"] = t.MaloaiSP;
              
                dt.Rows.Add(dr);
            }
            return dt;
        }

        public bool ThemSanPham(string strMaSP, string strTenSP, string strDVTinh, string strDonGia, string strMaLoaiSP)
        {
           
            bool f = false;
            try
            {
                SanPham sp = new SanPham()
                {
                    MaSP = strMaSP,
                    TenSP = strTenSP,
                    DonViTinh = strDVTinh,
                    DonGia = float.Parse(strDonGia),
                    MaloaiSP = strMaLoaiSP,
                   
                };
                dbs.SanPhams.InsertOnSubmit(sp);
                dbs.SubmitChanges();
                f = true;
            }
            catch
            {
                f = false;
            }
            return f;
        }
        public bool XoaSanPham(string strMaSP)
        {
            bool f = false;
            try
            {
                SanPham sanpham = dbs.SanPhams.Single(sp => sp.MaSP == strMaSP);
                dbs.SanPhams.DeleteOnSubmit(sanpham);
                dbs.SubmitChanges();
                f = true;
            }
            catch
            {
                f = false;
            }
            return f;
        }
        public bool CapNhatSanPham(string strMaSP, string strTenSP, string strDVTinh, string strDonGia, string strMaLoaiSP)
        {
            bool f = false;
            try
            {
              
                SanPham sanpham =
                    dbs.SanPhams.
                    Single(sp => sp.MaSP == strMaSP);
                sanpham.TenSP = strTenSP;
                sanpham.DonViTinh = strDVTinh;
                sanpham.DonGia = float.Parse(strDonGia);
                sanpham.MaloaiSP = strMaLoaiSP;
              
                dbs.SubmitChanges();
                f = true;
            }
            catch
            {
                f = false;
            }
            return f;

        }
    }
}
